package com.huya.k12.translate.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import com.huya.k12.translate.model.vo.WordVo;
import com.huya.k12.translate.service.WordService;
import com.huya.k12.translate.service.XMLService;
import com.huya.k12.translate.util.XMLUtil;


@Service
public class XMLServiceImpl implements XMLService{
	
	@Autowired
	private XMLUtil xmlUtil;
	
	@Autowired
	private WordService wordService;
	
	private static Logger logger = Logger.getLogger(XMLServiceImpl.class);
	
	
	@Override
	public String exportXML(String countryCode) {
		// TODO Auto-generated method stub
		String result = null;
		List<WordVo> list = wordService.wordVoList();
		Document document = null;
		if(list != null && list.size() > 0){
			document = DocumentHelper.createDocument();
			Element root = DocumentHelper.createElement("resources");
			document.setXMLEncoding("utf-8");
			document.setRootElement(root);
			for(WordVo wordVo : list){
				Map<String, String> map = wordVo.getLanguage();
				if(map != null){
					String value = map.get(countryCode);
					if(StringUtils.isBlank(value)){
						Element node = DocumentHelper.createElement("string");
						node.addAttribute("name", wordVo.getWordId());
						if(StringUtils.isBlank(wordVo.getLanguage().get("en")))
							logger.info("the word have not en:"+wordVo.getWordId());
						else
							node.setText(wordVo.getLanguage().get("en"));
						root.add(node);
					}
					
				}
			}
		}
		if(document != null){
			XMLWriter writer = null;
			try {
				OutputFormat format = OutputFormat.createPrettyPrint();
				format.setEncoding("utf-8");
				StringWriter sw = new StringWriter();
				writer = new XMLWriter(sw, format);
				writer.setEscapeText(true);
				writer.write(document);
				result = sw.toString();
				writer.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				if(writer != null)
					try {
						writer.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
		}
		return result;
	}

	@Override
	public void importXML(MultipartFile file, String countryCode) {
		// TODO Auto-generated method stub
		
//		if(file!=null&&!file.isEmpty()){
//			String filePath ="D://"+"/temp";
//			File xmlFile = new File(filePath+"/data.xml");
//			if(!xmlFile.getParentFile().exists())
//				xmlFile.getParentFile().mkdirs();
//			try {
//				file.transferTo(xmlFile);
//			} catch (IllegalStateException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			importWord(xmlFile, countryCode);
//		}
		if(file != null && ! file.isEmpty()){
			try {
				InputStream is = file.getInputStream();
				importWord(is, countryCode);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				logger.error(e.getMessage(),e);
			}
		}
	}
	
	private void importWord(InputStream inputStream, String countryCode){
		if(inputStream != null){
			Document document  = xmlUtil.getDocument(inputStream);
			if(document != null){
				Map<String, String> map = xmlUtil.read(document);
				if(map!=null&&map.size()>0){
					wordService.updateByCountryCode(map, countryCode);
				}
			}
		}
	}

}
